package com.frxuexi.park2210.repair

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.RepairBeanItem
import com.frxuexi.lib_base.entity.RepairCommentBean
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.net.Const
import com.frxuexi.park2210.R
import com.frxuexi.park2210.adapter.CommentAdapter
import com.frxuexi.park2210.databinding.ActivityRepairDetailBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/RepairDetailActivity")
@AndroidEntryPoint
class RepairDetailActivity : BaseActivity<ActivityRepairDetailBinding,RepairViewModel>() {

    @Autowired
    lateinit var repair:RepairBeanItem
    val commentAdapter by lazy { CommentAdapter() }
    var position=0
    override fun initData() {
        vm.addComentSuc.observe(this){
            ToastUtils.showLong("新增成功")
            Log.i("===",it.toString())
        }

        vm.getCommentsSuc.observe(this){
            Log.i("===",it.toString())
            commentAdapter.addData(it)
            commentAdapter.notifyDataSetChanged()
        }

        vm.deleteCommentsSuc.observe(this){
            Log.i("===",it.toString())
            commentAdapter.data.clear()
            vm.getComments(repair.repairId)
        }

        vm.updateCommentsSuc.observe(this){
            commentAdapter.data.get(position).content="哈哈"
            commentAdapter.notifyItemChanged(position)
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        Log.i("===",repair.toString())
        binding.repairTitle.append(repair.repairData)
        binding.repairContent.append(repair.repairDescribe)
        binding.repairUser.append(repair.userId.toString())

        if(repair.repairUserId!=0){
            binding.ivSh.setImageResource(R.drawable.ic_action_cheched)
            binding.repairSh.setText("已审核")
            binding.repairUserId.setText(repair.repairUserId.toString())
        }

        binding.btnAddCommemt.setOnClickListener {
            val comment = binding.etComment.text.toString()
            val bean = RepairCommentBean(
                null,
                repair.repairId,
                comment,
                SPUtils.getInstance().getString(Const.UNAME),
                vm.getDateTime(System.currentTimeMillis())
            )
            vm.addComment(bean)
            commentAdapter.addData(0,bean)
        }

        binding.rvComment.apply {
            adapter=commentAdapter
            layoutManager=LinearLayoutManager(this@RepairDetailActivity)
        }

        commentAdapter.setOnItemClickListener { adapter, view, position ->
            this.position=position
            val bean = commentAdapter.data.get(position)
            bean.content="哈哈"
            vm.updateComment(bean)
        }

        vm.getComments(repair.repairId)

        commentAdapter.setOnItemLongClickListener { adapter, view, position ->
            vm.deleteComment(commentAdapter.getItem(position))
            return@setOnItemLongClickListener true
        }


    }

}